﻿/**
 * 每周期
 */
function everyTime(dom) {
    var item = $("input[name=v_" + dom.name + "]");
    item.val("*");
    item.change();
}

/**
 * 每周期
 入参为元素的名字
 */
function everyTimeByName(v_name) {
    var item = $("input[name=" + v_name + "]");
    item.val("*");
    item.change();
}

//unselect the checkbox
function unselectAll(listName) {
    $("." + listName).children().each(function () {
        $(this).removeAttr("checked");
    });
}

/**
 * 不指定
 */
function unAppoint(dom) {
    var name = dom.name;
    var val = "?";
    if (name == "year")
        val = "";
    var item = $("input[name=v_" + name + "]");
    item.val(val);
    item.change();
}

/**
 * 不指定
 入参为元素的名字
 */
function unAppointByName(v_name) {

    var val = "?";
    if (v_name == "year")
        val = "";
    var item = $("input[name=" + v_name + "]");
    item.val(val);
    item.change();
}

function appoint(dom) {

}

/**
 * 周期
 */
function cycle(dom) {
    var name = dom.name;
    var ns = $(dom).parent().find(".numberspinner");
    var start = ns.eq(0).numberspinner("getValue");
    var end = ns.eq(1).numberspinner("getValue");
    var item = $("input[name=v_" + name + "]");
    item.val(start + "-" + end);
    item.change();
}

/**
 * 从开始
 */
function startOn(dom) {
    var name = dom.name;
    var ns = $(dom).parent().find(".numberspinner");
    var start = ns.eq(0).numberspinner("getValue");
    var end = ns.eq(1).numberspinner("getValue");
    var item = $("input[name=v_" + name + "]");
    item.val(start + "/" + end);
    item.change();
}

function lastDay(dom) {
    var item = $("input[name=v_" + dom.name + "]");
    item.val("L");
    item.change();
}

function weekOfDay(dom) {
    var name = dom.name;
    var ns = $(dom).parent().find(".numberspinner");
    var start = ns.eq(0).numberspinner("getValue");
    var end = ns.eq(1).numberspinner("getValue");
    var item = $("input[name=v_" + name + "]");
    item.val(start + "#" + end);
    item.change();
}

function lastWeek(dom) {
    var item = $("input[name=v_" + dom.name + "]");
    var ns = $(dom).parent().find(".numberspinner");
    var start = ns.eq(0).numberspinner("getValue");
    item.val(start + "L");
    item.change();
}

function workDay(dom) {
    var name = dom.name;
    var ns = $(dom).parent().find(".numberspinner");
    var start = ns.eq(0).numberspinner("getValue");
    var item = $("input[name=v_" + name + "]");
    item.val(start + "W");
    item.change();
}

$(function () {
    $(".numberspinner").numberspinner({
        onChange: function () {
            $(this).closest("div.line").children().eq(0).click();
        }
    });

//find object which prop "name" like "v_%"
    var vals = $("input[name^='v_']");
    var cron = $("#cron");
    vals.change(function () {
        var item = [];
        vals.each(function () {
            item.push(this.value);
        });
        cron.val(item.join(" "));
    });

    var secondList = $(".secondList").children();
    $("#sencond_appoint").click(function () {
        if (this.checked) {
            secondList.eq(0).change();
        }
    });

    secondList.change(function () {
        var sencond_appoint = $("#sencond_appoint").prop("checked");
        if (sencond_appoint) {
            var vals = [];
            secondList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 59) {
                val = vals.join(",");
            } else if (vals.length == 59) {
                val = "*";
            }
            var item = $("input[name=v_second]");
            item.val(val);
            item.change();
        }
    });

    var minList = $(".minList").children();
    $("#min_appoint").click(function () {
        if (this.checked) {
            minList.eq(0).change();
        }
    });

    minList.change(function () {
        var min_appoint = $("#min_appoint").prop("checked");
        if (min_appoint) {
            var vals = [];
            minList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 59) {
                val = vals.join(",");
            } else if (vals.length == 59) {
                val = "*";
            }
            var item = $("input[name=v_min]");
            item.val(val);
            item.change();
        }
    });

    var hourList = $(".hourList").children();
    $("#hour_appoint").click(function () {
        if (this.checked) {
            hourList.eq(0).change();
        }
    });

    hourList.change(function () {
        var hour_appoint = $("#hour_appoint").prop("checked");
        if (hour_appoint) {
            var vals = [];
            hourList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 24) {
                val = vals.join(",");
            } else if (vals.length == 24) {
                val = "*";
            }
            var item = $("input[name=v_hour]");
            item.val(val);
            item.change();
        }
    });

    var dayList = $(".dayList").children();
    $("#day_appoint").click(function () {
        if (this.checked) {
            dayList.eq(0).change();
        }
    });

    dayList.change(function () {
        var day_appoint = $("#day_appoint").prop("checked");
        if (day_appoint) {
            var vals = [];
            dayList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 31) {
                val = vals.join(",");
            } else if (vals.length == 31) {
                val = "*";
            }
            var item = $("input[name=v_day]");
            item.val(val);
            item.change();
        }
    });

    //select the objects with class is mouthList
    var mouthList = $(".mouthList").children();
    $("#mouth_appoint").click(function () {
        if (this.checked) {
            mouthList.eq(0).change();
        }
    });

    mouthList.change(function () {
        var mouth_appoint = $("#mouth_appoint").prop("checked");
        if (mouth_appoint) {
            var vals = [];
            mouthList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 12) {
                val = vals.join(",");
            } else if (vals.length == 12) {
                val = "*";
            }
            var item = $("input[name=v_mouth]");
            item.val(val);
            item.change();
        }
    });


    //add by EM3 2015
    // add the quarter module
    //select the objects with class is quarterList
    var quarterList = $(".quarterList").children();
    var monthList = $(".monthList").children();
    $("#quarter_appoint").click(function () {
        if (this.checked) {
            quarterList.eq(0).change();
            monthList.eq(0).change();
        }
    });


    //add by EM3 2015
    // add the quarter module
    //when quarter is selected , change the month select by the same time
    // the month select can be changed again
    quarterList.change(function () {
        var quarter_appoint = $("#quarter_appoint").prop("checked");
        if (quarter_appoint) {
            var vals = [];
            quarterList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            var v_ck = val;
            if (vals.length > 0 && vals.length < 3) {
                val = vals.join(",");
                v_ck = val;
            } else if (vals.length == 3) {
                val = "*";
                v_ck = vals.join(",");
            }
            var item = $("input[name=v_mouth]");
            item.val(val);
            item.change();
        }
        monthchange(v_ck);
    });

    monthList.change(function () {
        var quarter_appoint = $("#quarter_appoint").prop("checked");
        if (quarter_appoint) {
            var vals = [];
            monthList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 12) {
                val = vals.join(",");
            } else if (vals.length == 12) {
                val = "*";
            }
            var item = $("input[name=v_mouth]");
            item.val(val);
            item.change();
        }
    });

    //add by EM3 2015
    //change the month when the quarter is selected
    function monthchange(val) {
        //first unselect all the monthlist
        unselectAll('monthList');

        //then check the select value
        ary = val.split(",");
        for (var i = 0; i < ary.length; i++) {
            $(".monthList input[value='" + ary[i] + "']").attr("checked", "checked");
        }

    }


    var weekList = $(".weekList").children();
    $("#week_appoint").click(function () {
        if (this.checked) {
            weekList.eq(0).change();
        }
    });

    weekList.change(function () {
        var week_appoint = $("#week_appoint").prop("checked");
        if (week_appoint) {
            var vals = [];
            weekList.each(function () {
                if (this.checked) {
                    vals.push(this.value);
                }
            });
            var val = "?";
            if (vals.length > 0 && vals.length < 7) {
                val = vals.join(",");
            } else if (vals.length == 7) {
                val = "*";
            }
            var item = $("input[name=v_week]");
            item.val(val);
            item.change();
        }
    });
});
